<?php
class Cart extends Controller
{
    public function __construct()
    {
        parent::__construct();

        self::$crossParams['title'] = 'Giỏ hàng';

        $this->setBreadcrumb(array(
            array(
                'title' => 'Trang chủ',
                'link' => Link::build(''),
                'index' => true,
            ),
            array(
                'title' => 'Giỏ hàng',
                'link' => Link::build('cart/detail')
            )
        ));

        $this->loadChildControllers(array(
            // header & footer
            'header' => 'module/header',
            'footer' => 'module/footer',

            // left siderbar
            'quick_search' => 'module/QuickSearch',
            'category'      => 'module/Category',
            'contact_info'  => 'module/ContactInfo',
            'support'       => 'module/Support',
        ));
    }

    public function actionDelete($productId)
    {
        $cartModel = $this->_loader->model('CartModel');

        $cartModel->delete($productId);

        return $this->responseRedirect('cart/detail');
    }

    /**
     * Show detail of the cart
     * @return null|Response
     */
    public function actionDetail()
    {
        $cartModel = $this->_loader->model('CartModel');

        $defaultStyle = $this->_loader->config('default_style');

        $cart = $cartModel->getCart();

        $totalPrice = 0;
        foreach ($cart as $item)
        {
            $totalPrice += $item['price'] * $item['quantity'];
        }

        /* Params */
        $viewParams = array(
            'cart' => $cart,
            'totalPrice' => $totalPrice,
        );

        return $this->responseView('cart/detail', $viewParams, 'layout/left_sidebar');
    }

    /**
     * Add product to the cart
     * @param $productId
     */
    public function actionAdd($productId)
    {
        // Get product
        $productModel = $this->_loader->model('ProductModel');
        $product = $productModel->getProductById($productId);

        if (!$product)
        {
            $viewParams = array(
                'error' => 'Sản phẩm không tồn tại!'
            );

            return $this->responseView('error', $viewParams, 'layout/left_sidebar');
        }

        $cartModel = $this->_loader->model('CartModel');

        $productImages = explode(',', $product['url_images']);

        $cartModel->updateItem(array(
            'id' => $product['product_id'],
            'title' => $product['name'],
            'quantity' => 1,
            'price' => $product['price'],
            'image' => isset($productImages[0])? $productImages[0] : false
        ));

        return $this->responseRedirect('cart/detail');
    }

    public function actionUpdate()
    {
        $quantities = $this->_input->filterSingle('quantity', Input::ARRAY_SIMPLE);

        $cartModel = $this->_loader->model('CartModel');

        $cartModel->updateQuantity($quantities);

        return $this->responseRedirect('cart/detail');
    }

    public function actionShortDetail()
    {
        $cartModel = $this->_loader->model('CartModel');

        $cart = $cartModel->getCart();

        $totalPrice = 0;
        foreach ($cart as $item)
        {
            $totalPrice += $item['price'] * $item['quantity'];
        }

        $viewParams = array(
            'totalItems' => count($cart),
            'totalPrice' => $totalPrice,
        );

        return $this->responseView('cart/short_detail', $viewParams);
    }
}